package com.guangsi.compute;

import org.hyperic.sigar.SigarException;

import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class CpuCtl {
    static int cpuCount = 0;
    static List<ComputeProcess> computeProcessesList = new ArrayList();
    static CountDownLatch latch;
    static Object lock;

    public CpuCtl() {
    }

    private static void doCpuCompute(Long hope) {
        double cpuUsed = CpuUtils.getInstance().getCpuUsage();
        float hopePersent = (float) (hope / 100.00);
        System.out.println("hope persent:" + hopePersent);
        System.out.println("current cpu used:" + cpuUsed);
        if (computeProcessesList.size() <= 0 || cpuUsed <= (hopePersent - 0.05) || cpuUsed >= (hope + 0.05)) {
            System.out.println("##########shutdown computing process########");
            Iterator it = computeProcessesList.iterator();
            ComputeProcess process;
            while (it.hasNext()) {
                process = (ComputeProcess) it.next();
                process.exit();
                process.interrupt();
            }
            try {
                if (computeProcessesList.size() > 0) {
                    latch.await();
                }
            } catch (InterruptedException var6) {
                var6.printStackTrace();
            }
            System.out.println("##########shut down computing process success########");
            computeProcessesList.clear();
            latch = null;
            try {
                TimeUnit.SECONDS.sleep(1L);
            } catch (InterruptedException var5) {
                var5.printStackTrace();
            }
            cpuUsed = CpuUtils.getInstance().getCpuUsage();
            latch = new CountDownLatch(cpuCount);
            System.out.println("ready rebuild computing process:" + cpuUsed);
            if (cpuUsed > (hope + 0.05)) {
                System.out.println("cpu use than 72% : current:" + cpuUsed + ",wait for next turn");
            } else {
                System.out.println("########## rebuild computing process########");
                for (int i = 0; i < cpuCount; ++i) {
                    computeProcessesList.add(new ComputeProcess(hopePersent, latch));
                }
                it = computeProcessesList.iterator();
                while (it.hasNext()) {
                    process = (ComputeProcess) it.next();
                    process.start();
                }
                System.out.println("##########rebuild computing process success########");
            }
        }

    }

    public static void main(String[] args) {
        System.out.println("cpuCtl 程序执行开始-------\n\n");
        final Long hope = (args == null || args.length < 1) ? 40L : Long.valueOf(args[0]);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                CpuCtl.doCpuCompute(hope);
            }
        }, 0L, 20000L);
    }

    static {
        try {
            cpuCount = CpuUtils.getInstance().getSigar().getCpuInfoList().length;
        } catch (SigarException var1) {
            var1.printStackTrace();
        }
        latch = new CountDownLatch(cpuCount);
        lock = new Object();
    }
}
